/*添加角色*/
var addGroup = new Vue({
	el: "#qxgl_addGroup",
	data: function() {
		//自定义验证，验证机构编码
		var checkId = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入群组编码'));
			}
			setTimeout(function() {
				//中文判断
				if(!/^[a-zA-Z0-9_]{0,}$/.test(value)) {
					callback(new Error('编码不能为中文字符'));
				} else {
					callback();
				}
			});
		};
		//验证机构号码
		var checkofficeTel = function(rule, value, callback) {
			if(!value) {
				callback();
			}
			setTimeout(function() {
				//数字判断
				if(!/^[0-9]*$/.test(value)) {
					callback(new Error('电话只能是数字'));
				} else {
					callback();
				}
			});
		};
		return {
			addGroupVisible: false,
			labelPosition: 'right',
			isEdit: false,
			ruleForm: {
				teamCode: '',
				id: '',
				name: '',
				officeTel: '',
				state: 1
			},
			rules: {
				id: {
					required: true,
					validator: checkId,
					trigger: 'blur'
				},
				name: {
					required: true,
					message: '请输入群组名称',
					trigger: 'blur'
				}
			}
		};
	},
	computed: {
		title: function(){
			return this.isEdit ? "编辑群组" : "修改角色"
		}
	},
	methods: {
		submitForm: function(formName, e) {
			var self = this;
			this.$refs[formName].validate(function(valid) {
				if(valid) {
					var url = self.isEdit ? "/base/team/modify" : "/base/team/add"
					VanyAjax(url, {
						teamName: self.ruleForm.name,
						teamCode: self.ruleForm.teamCode
					}).done(function(data) {
						this.treeObj.getData(this.treeObj.url, {
							token: sessionStorage.token
						});
						this.hide();
						this.resetForm("ruleForm");
					}, true, self);
				} else {
					console.log('error submit!!');
					VanyAjax.prototype.hideLoading();
					e.stopPropagation();
					return false;
				}
			});
		},
		show: function(treeObj){
			if(treeObj){
				this.treeObj = treeObj;
			}
			this.addGroupVisible = true;
		},
		hide: function(){
			this.addGroupVisible = false;
		},
		handleClose: function(done) {
			done();
		},
		//重置
		resetForm: function(formName) {
			this.ruleForm.name = "";
			this.$refs[formName].resetFields();
		}
	}
});

var groupPerson = new Vue({
	el: "#groupPerson",
	data: function() {
		return {
			//url: "/templates/authority/qxgl/json/rolePerson.json",
			url: "/base/team/queryTeamer",
			params: {
				teamCode: "",
				token: sessionStorage.token,
				pageIndex: 1,
				pageSize: 100,
				searchVal: ""
			},
			nodataFlag: false,
			groupPersonVisible: false,
			tableData: [],
			sels: [],
			firstLoad: true
		}
	},
	methods: {
		/**
		 * 函数节流器
		 */
		_throttle: function(method, context) {
			clearTimeout(method.tId);
			var _arguments_ = arguments;
			method.tId = setTimeout(function() {
				var args = [];
				for(var i = 1; i < _arguments_.length; i++) {
					args.push(_arguments_[i]);
				}
				method.apply(this, args);
			}, 100);
		},
		/**
		 * 判断一个可滚动元素是否滚动到了底部
		 */
		_isScrollToBottom: function(e, fn) {
			// e.srcElement存在兼容性问题，并非标准特性
			// https://developer.mozilla.org/zh-CN/docs/Web/API/Event/target
			// 支持 IE6-8
			var target_ = e.target || e.srcElement,
				// 距离顶部高度
				scrollTop = target_.scrollTop,
				// 滚动区域高度
				scrollHeight = target_.scrollHeight,
				// 文档元素高度
				clientHeight = target_.clientHeight;
			// 文档坐标和视口坐标转换，判断元素滚动到了底部
			if(scrollTop + clientHeight == scrollHeight) {
				if(fn) {
					fn.call(this);
				}
			}
		},
		show: function(){
			this.groupPersonVisible= true;
		},
		hide: function(){
			this.groupPersonVisible = false;
		},
		// 人员筛选器
		openPersonFilter: function(){
			var self = this;
			if(!this.personFilter){
				this.personFilter = VanyFilter({
					mountEl: "#content",
					title: "选择人员",
					tabs: [{
						"name": "人员",
						"id": "renyuan",
						"filterUrl": "/base/userCondition/getCondition",
						"url": "/base/userCondition/getBaseUserInfo",
						"pagination": true,
						"isPersonSeleted": true,
						"defaultProps": {
							"children": 'children',
							"label": 'userName',
							"id": "userCode"
						}
					}],
					ok: function() {
						VanyAjax("/base/team/addTeamUsers", {
							teamCode: self.params.teamCode,
							userCodes: this.tabs[0].allNodesId.join(",")
						}, "POST", ".vanymodal_vueversion").done(function(data){
							this.personFilter.hide();
							this.personFilter.destroy();
							this.personFilter = null;
							this.resetPaginationParams();
							this.getData();
						}, true, self);
						return false;
					}
				});
			}else{
				this.personFilter.show();
			}
		},
		filterTable: function(){
			this.resetPaginationParams();
			this.getData();
		},
		restore: function(){
			if(this.params.searchVal.length===0){
				this.resetPaginationParams();
				this.getData();
			}
		},
		getData: function() {
			VanyAjax(this.url, this.params, "GET", "#groupPerson .el-table").done(function(data) {
				if(data.total === 0) {
					this.nodataFlag = true;
					return;
				}
				if(this.params.pageIndex * this.params.pageSize >= data.total) {
					this.nodataFlag = true;
				}
				this.tableData = this.tableData.concat(data.rows);
				this.params.pageIndex++;
				if(this.firstLoad){
					var self = this;
					var tablebody_ = this.$refs["table"].$el;
					tablebody_ = tablebody_.getElementsByClassName("el-table__body-wrapper")[0];
					tablebody_.onscroll = function(e) {
						self._throttle(self._isScrollToBottom, e, self.getData);
					}
					this.firstLoad = false;
				}
			}, false, this);
		},
		resetPaginationParams: function() {
			this.tableData = [];
			this.params.pageIndex = 1;
			this.nodataFlag = false;
			this.sels = [];
		},
		remove: function() {
			var self = this;
			self.$confirm("是否进行批量删除操作", '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function() {
				VanyAjax("/base/team/delTeamUsesr", {
					teamCode: self.params.teamCode,
					teamUserCodes: self.sels.map(function(item){
						return item.userTeamId;
					}).join(",")
				}).done(function(data){
					for(var i = 0; i < this.sels.length; i++) {
						for(j = 0; j < this.tableData.length; j++) {
							if(this.sels[i].account_number === this.tableData[j].account_number) {
								this.tableData.splice(j, 1);
								break;
							}
						}
					}
				}, true, self);
			});
		},
		selsChange: function(sels) {
			this.sels = sels;
		},
		handleClose: function(){
			this.resetPaginationParams();
			this.params.searchVal = "";
			try{
				this.personFilter.destroy();
				this.personFilter = null;
			}catch(e){
			}
		}
	}
});